#include <QtGui>


#include "droparea.h"
#include "mainwindow.h"

DropArea::DropArea(QWidget *parent)
    : QLabel(parent)
{
    // 134 e' la larghezza della finestrella
    // TODO: parametrizzare questi valori --> skinnabile
    setMinimumSize(134, 134);
    move(50, 61);
    //setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
    setAlignment(Qt::AlignCenter);
    setAcceptDrops(true);
    //setAutoFillBackground(true);
    quitted = FALSE;
    loaded = FALSE;
    clear();
}

void DropArea::dragEnterEvent(QDragEnterEvent *event)
{
    setStyleSheet(
	  "color: white;"
	  "font: bold italic 14px;"
    );
	
    setText(tr("<drop'd>"));
    event->acceptProposedAction();
    emit changed(event->mimeData());
}

void DropArea::dragMoveEvent(QDragMoveEvent *event)
{
	event->acceptProposedAction();
}

void DropArea::load()
{
	 loaded = TRUE;
	 clear();
}

void DropArea::unload()
{
	 loaded = FALSE;
	 clear();
}

void DropArea::dropEvent(QDropEvent *event)
{
	const QMimeData *mimeData = event->mimeData();

	QString path = mimeData->urls().first().path();
	
	QFileInfo fi(path);

	if (fi.suffix() == "hrm") {
		MainWindow* mw = qobject_cast<MainWindow *>(parentWidget());
		if (mw->loadFile(fi))
			load();
		clear();
	}
  	else {
    		setStyleSheet(
		  "color: red;"
		  "font: bold italic 14px;"
		  );
		setText(tr("not .hrm file"));
    		QApplication::beep();
		QTimer::singleShot(2000, this, SLOT(clear())); 
  	}
	
	event->acceptProposedAction();
}

void DropArea::dragLeaveEvent(QDragLeaveEvent *event)
{
    clear();
    event->accept();
}

void DropArea::quitting()
{
	quitted = TRUE;
	setStyleSheet(
		"color: white;"
		"font: italic 14px;"
	);
	
	setText(tr("Quitting..."));
}

void DropArea::notQuitting()
{
	quitted = FALSE;
	clear();
}


void DropArea::clear()
{
	if (!quitted) {
		if (!loaded) {
			setStyleSheet("color: white;"
				      "font: italic 14px;"
			);
			
			setText(tr("<drop here>"));
		}
		else {
			setStyleSheet("color: yellow;"
				      "font: bold italic 14px;"
			);
			setText(tr("<Loaded>"));	
		}
	}
	//emit changed();
}
